//
// Created by Administrator on 2021/10/20.
//

#include <iostream>
#include <vector>
#include <climits>


using namespace std;

class Solution {
public:
    int nearestValidPoint(int x, int y, vector<vector<int>> &points) {
        int ans = -1, minRange = INT_MAX;
        for (int i = 0; i < points.size(); ++i) {
            if (points[i][0] == x or points[i][1] == y) { // 有效点
                int mRange = abs(points[i][0] - x) + abs(points[i][1] - y); // 曼哈顿距离
                if (mRange < minRange) { // 更新
                    ans = i;
                    minRange = mRange;
                }
            }
        }
        return ans;
    }
};

int main() {
    vector<vector<int>> points{{1, 2},
                               {3, 1},
                               {2, 4},
                               {2, 3},
                               {4, 4}};
    Solution solution;
    cout << solution.nearestValidPoint(3, 4, points) << endl;
    return 0;
}